package cc.rengu.igas.smps.core.packswitch;

import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.smps.common.constant.SmpsTreeNodeConstant;
import cc.rengu.igas.smps.common.enums.RespCodeEnum;
import cc.rengu.jradp.packetswitch.IncomingPackSwitchImpl;
import cc.rengu.jradp.packetswitch.OutgoingPackSwitchImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.SHAUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import org.springframework.util.StringUtils;

/**
 * 开发者平台&支付2.0服务器通讯层报文解包前后、组前包后实现类
 */
public class DevelopClientPackSwitch implements IncomingPackSwitchImpl, OutgoingPackSwitchImpl {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public byte[] beforeInComPackSwitch(byte[] bytes) throws Exception {
        String content = new String(bytes);
        // 不存在报文直接抛出异常
        if (StringUtils.isEmpty(content)) {
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                    RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        return bytes;
    }

    @Override
    public int afterInComPackSwitch() throws Exception {


        return 0;
    }

    @Override
    public int beforeOutgoPackSwitch() throws Exception {
        return 0;
    }

    @Override
    public byte[] afterOutgoPackSwitch(byte[] bytes) throws Exception {
        RgLogger rglog = RgLog.getLogger(this.getClass().getName());
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String content = new String(bytes);
        xmlTreeUtil.setXmlTreeStringValue(SmpsTreeNodeConstant.DEFINE_HTTP_SYSTEM_ID, ShareParamConstant.UOP_PAY_SYS_ID);
        //生成签名
        genSignStr(content);
        xmlTreeUtil.setXmlTreeStringValue("_MESSAGEHEAD/http/Content-Type", "application/json;charset=utf-8");
        xmlTreeUtil.setXmlTreeStringValue("_MESSAGEHEAD/http/ArgList", "X-Uop-Global-Trace-No:X-Uop-System-Id:X-Uop-Inner-Signature:Content-Type");
        if ("UOPCB0001".equals(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.HEADER_TXN_NUM))) {
            xmlTreeUtil.setXmlTreeStringValue("_MESSAGEHEAD/http/ArgList", "X-Uop-Unique-No:X-Uop-Api-Code:X-Uop-Notify-Url:X-Uop-App-Id:X-Uop-Biz-Id:X-Uop-Global-Trace-No:X-Uop-System-Id:X-Uop-Inner-Signature:Content-Type");
        }

        rglog.trace("request = <{}>", new String(bytes));
        return bytes;
    }

    private void genSignStr(String body) throws Exception {
        // JSONObject jsonContent = JSONObject.parseObject(body);
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        // 排序
        //JSONObject sortJsonContent = sortJsonObj(jsonContent);
        //rglog.trace("sortJsonContent =<{}> " ,JSONObject.toJSONString(sortJsonContent));
        StringBuffer sb = new StringBuffer();
        sb.append("Body=");
        sb.append(body);
        sb.append("&X-Uop-Global-Trace-No=");
        sb.append(xmlTreeUtil.getXmlTreeStringValue(SmpsTreeNodeConstant.DEFINE_GLOBAL_TRACE_NO));
        sb.append("&X-Uop-System-Id=");
        sb.append(xmlTreeUtil.getXmlTreeStringValue(SmpsTreeNodeConstant.DEFINE_HTTP_SYSTEM_ID));
        sb.append("&MacKey=");
        // 系统参数取MACKkey
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID,
                "DEVELOPER_MAC_KEY",
                "MAC_KEY");
        sb.append(sysParam.getParamValue());
        rglog.debug("signString = <{}>", sb.toString());
        // 签名数据放入内存
        xmlTreeUtil.setXmlTreeStringValue("_MESSAGEHEAD/http/X-Uop-Inner-Signature", SHAUtil.generateSignString(null, sb.toString()));
    }

}
